﻿using System;
using System.Collections.Generic;
using System.Data;
using WebOS.Modle;
using Asxsyd92Core.Utils.Data;
using Asxsyd92Core.Utils.Provider;
using System.Data.SqlClient;
using Asxsyd92Core.Utils;

namespace WebOS.Dal
{
    public class WorkFlowTaskDal : BaseRepository<WorkFlowTask>
    {

        public static WorkFlowTaskDal Instance
        {
            get { return SingletonProvider<WorkFlowTaskDal>.Instance; }
        }


        /// <summary>
        /// 删除临时任务
        /// </summary>
        /// <param name="flowID"></param>
        /// <param name="stepID"></param>
        /// <param name="groupID"></param>
        /// <param name="prevStepID"></param>
        /// <returns></returns>
        public int DeleteTempTasks(Guid flowID, Guid stepID, Guid groupID, Guid prevStepID)
        {
            string sql = "DELETE WorkFlowTask WHERE FlowID=@FlowID AND StepID=@StepID AND GroupID=@GroupID AND Status=-1";
            List<SqlParameter> parameters = new List<SqlParameter>(){
                new SqlParameter("@FlowID", SqlDbType.UniqueIdentifier){ Value = flowID },
                new SqlParameter("@StepID", SqlDbType.UniqueIdentifier){ Value = stepID },
                new SqlParameter("@GroupID", SqlDbType.UniqueIdentifier){ Value = groupID }
            };
            if (!prevStepID.IsEmptyGuid())
            {
                sql += " AND PrevStepID=@PrevStepID";
                parameters.Add(new SqlParameter("@PrevStepID", SqlDbType.UniqueIdentifier) { Value = prevStepID });
            }

            return Asxsyd92Core.Utils.Data.DbUtils.ExecuteNonQuerySp(sql, null);
            //WorkFlowTaskDal.Instance.GetList();
            //  return dbHelper.Execute(sql, parameters.ToArray());
        }

        public static int UpTabel(string table, string fied, string vaule, string zj, string ID)
        {

            string sql = "UPDATE " + table + " SET " + fied + "='" + vaule + "' where " + zj + "='" + ID + "'";
            WorkFlowTaskDal.Instance.GetList(sql, null);
            return 1;//Asxsyd92Core.Utils.Data.DbUtils.UpdateNoMolde(sql, null);
        }
        public static int DeleteTabel(string table, string zj, string ID)
        {

            string sql = "DELETE " + table + " where " + zj + "=@ID";
            return Asxsyd92Core.Utils.Data.DbUtils.ExecuteNonQuerySp(sql, new { vaule = zj, ID = ID });
        }
        public static int DeleteInstance(Guid flowID, Guid groupID)
        {

            string sql = "DELETE FROM WorkFlowTask WHERE GroupID=@GroupID";
            List<SqlParameter> parameters = new List<SqlParameter>(){
                new SqlParameter("@GroupID", SqlDbType.UniqueIdentifier){ Value = groupID }
            };
            if (!flowID.IsEmptyGuid())
            {
                sql += " AND FlowID=@FlowID";
                parameters.Add(new SqlParameter("@FlowID", SqlDbType.UniqueIdentifier) { Value = flowID });
            }
            return Asxsyd92Core.Utils.Data.DbUtils.ExecuteNonQuerySp(sql, null);
        }
    }
}
